package cn.btks.backstagemanage.menu.repository;

import cn.btks.backstagemanage.menu.pojo.Menu;
import cn.btks.backstagemanage.menu.pojo.MenuRolesDto;
import cn.btks.backstagemanage.role.pojo.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author lijun
 * @Date 2021/1/9 14:34
 */
@Repository
public interface MenuRepository extends JpaRepository<Menu,Long>, JpaSpecificationExecutor<Menu> {

    /**
     * 根据角色ID查询资源清单
     * @param roleId
     * @return
     */
    @Query(value = "SELECT * FROM menu m LEFT JOIN menu_roles mr ON m.id = mr.menu_id WHERE mr.roles_id = ?1",nativeQuery = true)
    List<Menu> findAllByRoleId(Long roleId);

    /**
     * 根据roleId查询加密后的资源信息
     * @param roleId
     * @return
     */
    @Query(value = "select menu_id from menu_roles where roles_id = ?1 group by menu_id",nativeQuery = true)
    List<String> findAllMenuIdByRoleId(String roleId);

    /**
     * 查询ID列表中的资源清单
     * @param idList
     * @return
     */
    @Query(value = "select * from menu where identity in ?1",nativeQuery = true)
    List<Menu> findAllByIdList(List<Long> idList);

    /**
     * 根据上级ID查询资源清单
     * @param id
     * @return
     */
    List<Menu> findAllByParentId(Long id);

    /**
     * 根据资源ID查询关联表数量
     * @param menuId
     * @return
     */
    @Query(value = "select count(*) from menu_roles where menu_id = ?1",nativeQuery = true)
    int queryCountMenuRoles(Long menuId);

    /**
     * 根据资源ID查询关联表数量（加密）
     * @param menuId
     * @return
     */
    @Query(value = "select count(*) from menu_roles where menu_id = ?1",nativeQuery = true)
    int queryCountMenuRoles1(String menuId);

    /**
     * 根据资源ID删除关联表数据
     * @param menuId
     * @return
     */
    @Modifying
    @Query(value = "delete from menu_roles where menu_id = ?1",nativeQuery = true)
    int deleteMenuRolesByMenuId(Long menuId);

    /**
     * 根据资源ID删除关联表数据（加密）
     * @param menuId
     * @return
     */
    @Modifying
    @Query(value = "delete from menu_roles where menu_id = ?1",nativeQuery = true)
    int deleteMenuRolesByMenuId1(String menuId);

    /**
     * 根据角色ID删除关联表数据
     * @param rolesId
     * @return
     */
    @Modifying
    @Query(value = "delete from menu_roles where roles_id = ?1",nativeQuery = true)
    int deleteMenuRolesByRolesId(Long rolesId);

    /**
     * 根据角色ID删除关联表数据（加密）
     * @param rolesId
     * @return
     */
    @Modifying
    @Query(value = "delete from menu_roles where roles_id = ?1",nativeQuery = true)
    int deleteMenuRolesByRolesId1(String rolesId);

    /**
     * 根据ID删除资源及其关联信息
     * @param id
     * @return
     */
    @Modifying
    @Query(value = "delete from menu where id = ?1",nativeQuery = true)
    int deleteMenuById(Long id);

    /**
     * 根据角色ID判断是否存在关联资源清单
     * @param rolesId
     * @return
     */
    @Query(value = "select count(*) from menu_roles where roles_id = ?1",nativeQuery = true)
    int isExistMenuRoles(Long rolesId);

    /**
     * 根据角色ID判断是否存在关联资源清单（加密）
     * @param rolesId
     * @return
     */
    @Query(value = "select count(*) from menu_roles where roles_id = ?1",nativeQuery = true)
    int isExistMenuRoles1(String rolesId);
}
